import json
import re
import time

import requests
from urllib.parse import quote

with open('../审批事项配置/cookie.json','r',encoding='utf-8') as f:
    headers = json.load(f)

def queryItem(item_code='',regionCode='',itemType=''):
    data = {
        '_search': 'true',
        'nd': int(time.time() * 1000),
        'rows': '1000',
        'page': '1',
        'sidx': 'CODE',
        'sord': 'desc',
        'icode': item_code,
        'iname': '',
        'iorg': '',
        'iorgss': '',
        'regionCode': regionCode,
        'assort': '0',
        'itemType': itemType,
        'subType': '',
        'useInnerCode': '0',
        'Item': '',
        'isNewItem': '',
    }

    response = requests.post(
        'http://59.215.206.188/tyspzy/approval/systemsetting/approvalitem/query',

        headers=headers,
        data=data,
        verify=False,
    )
    # print(response.text)
    lists = []
    for i in response.json()['rows']:
        if i['cell']['TYPE'] !="XN" and 'FORM' not in i['cell']:


            lists.append([i['cell']['ITEM_ID'],#0
                          i['cell']['CODE'],
                          i['cell']['AGENT_CODE'],
                          i['cell']['AGENT_NAME'],#3
                          i['cell']['REGION_CODE'],
                          i['cell']['REGION_NAME'],
                          i['cell']['NAME'],#6
                          i['cell']['TYPE'],
                          i['cell']['VERSION'],#8
                          i['cell']['AGREE_TIME'],#9
                          ])


    # lists2=[]
    print(lists)
    # # for i in lists:
    # #     lists2.append(save(i))
    # # print(lists2)
    return lists


def save(lists):

    params = {
        'method': 'saveExt',
    }

    data = {
	"itemId": lists[0],
	"itemCode": lists[1],
	"ishall": "0",
	"isExchange": "0",
	"fileMustUpload": "0",
	"licenseFileMustUpload": "",
	"noSubmitTime": "0",
	"isImmediate": "",
	"type": "1,2,3",
	"formId": "1089968970515087360",
	"formType": "SAAS",
	"formObjectNew": "",
	"publicAutoCode": "",
	"formName": "业务表单",
	"waringTime": "0",
	"waringTimeUnit": "day",
	"subProcessLimitTime": "",
	"subProcessLimitTimeUnit": "day",
	"subNodeId": "",
	"itemTime": lists[9],
	"itemTimeUnit": "workday",
	"orgCode": lists[2],
	"orgName": lists[3],
	"regCode": lists[4],
	"regName": lists[5],
	"isFreeTradeZone": "",
	"isSetUp": "0",
	"projectType": "00",
	"officialType": "",
	"isImportant": "0",
	"itemName": quote(quote(lists[6])),
	"itemHandleName": "",
	"itemAssort": "1",
	"itemIsPyc": "",
	"agentName": lists[3],
	"itemType": lists[7],
	"subType": "",
	"agentCode": lists[4],
	"isUniteCert": "",
	"isHiddenForm": "0",
	"isPublicity": "1",
	"isCorrectionApply": "0",
	"version": lists[8],
	"provinceItemCode": "",
	"push_val": [],
	"fileType": "",
	"smartItem": "",
	"isBuild": "0",
	"acceptanceCheck": "0",
	"nodeGroup": [],
	"preliminaryApproval": "0",
	"itemAttribute": "",
	"supervisePowerLevel": "",
	"isProvinceLicense": "0"
}
    responses = requests.post(
        'http://59.215.206.188/tyspzy/approval/systemsetting/approvalitem',
        params=params,
        headers=headers,
        json=data,
        verify=False,
    ).text
    # print(responses)
    return responses

print(queryItem(regionCode='520323'))